<template>

  <iframe :id='id' :src='url' frameborder='0' width='100%' height='800px' scrolling='auto'></iframe>

</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN, YEAR_SEMESTER } from '@/store/mutation-types'
import PageLayout from '../page/PageLayout'
import RouteView from './RouteView'
import { getAction } from '@api/manage'
import { getSysThirdOauthRelationship } from '@api/commonAPI'

export default {
  name: 'IframePageContent',
  inject: ['closeCurrent'],
  data() {
    return {
      url: '',
      id: '',
      xkwOpenId: ''
    }
  },
  created() {
    this.getxkwOpenId()
    this.goUrl()
  },
  updated() {
    this.goUrl()
  },
  watch: {
    $route(to, from) {
      this.goUrl()
    }
  },
  methods: {

    getxkwOpenId(){
      let url;
      if(this.url){
        url = this.url;
      }else{
        url = this.$route.meta.url
      }

      // 第三方接入OpenId变量替换
      let xkwOpenIdStr = '${xkwOpenId}'
      if (url.indexOf(xkwOpenIdStr ) != -1 && ! this.xkwOpenId) {
        let getOpenIdByLoginUserUrl = "/oauth/xkwoauth/sysThirdOauthRelationship/getOpenIdByLoginUser"
        getAction(getOpenIdByLoginUserUrl).then((res)=>{
          if (res.success) {
            let openId = res.result.thirdOpenid
            this.xkwOpenId = openId;
            url = url.replace(xkwOpenIdStr, openId);
            console.log('--初始化xkwOpenId----getxkwOpenId:this.xkwOpenId------' + this.xkwOpenId)
            console.log('--初始化替换xkwOpenId----getxkwOpenId:url------' + url)
            this.url = url;
            console.log('--初始化替换结果----getxkwOpenId:this.url------' + this.url)
            this.goUrl();//再次进入
          }else {
            console.log('------getxkwOpenId:res.message------' + res.message)
            this.$message.warning(res.message)
          }
        })
      }
      else if(url.indexOf(xkwOpenIdStr ) != -1 &&  this.xkwOpenId){
        url = url.replace(xkwOpenIdStr, this.xkwOpenId);
        console.log('--正常替换xkwOpenId----getxkwOpenId:url------' + url)
        this.url = url;
        console.log('--替换结果----getxkwOpenId:this.url------' + this.url)
        this.goUrl();//再次进入
      }
      else{
        console.log('--无需替换----getxkwOpenId:this.url------' + this.url)
      }

      return url;
    },
    goUrl() {
      let url;
      if(this.url){
        url = this.url;
      }else{
        url = this.$route.meta.url
      }
      let id = this.$route.path
      this.id = id
      //url = "http://www.baidu.com"
      console.log('------goUrl:url------' + url)
      if (url !== null && url !== undefined) {
        //-----------------------------------------------------------------------------------------
        //url支持通过 ${token}方式传递当前登录TOKEN
        let tokenStr = '${token}'
        let schoolYearIdStr = '${schoolYearId}'
        let semesterIdStr = "${semesterId}";
        if (url.indexOf(tokenStr) != -1) {
          let token = Vue.ls.get(ACCESS_TOKEN)
          let schoolYearId = Vue.ls.get(YEAR_SEMESTER).schoolYearId
          let semesterId = Vue.ls.get(YEAR_SEMESTER).semesterId
          url = url.replace(tokenStr, token).replace(schoolYearIdStr, schoolYearId).replace(semesterIdStr, semesterId);
        }
        this.url = url

        //第三方接入OpenId变量替换
        let xkwOpenIdStr = '${xkwOpenId}'
        if (url.indexOf(xkwOpenIdStr) != -1) {
          if(this.xkwOpenId) {
            url = url.replace(xkwOpenIdStr, this.xkwOpenId);
          }else{
            url = this.getxkwOpenId();
          }
        }

        //-----------------------------------------------------------------------------------------

        /*update_begin author:wuxianquan date:20190908 for:判断打开方式，新窗口打开时this.$route.meta.internalOrExternal==true */
        if (this.$route.meta.internalOrExternal != undefined && this.$route.meta.internalOrExternal == true) {
          this.closeCurrent()
          window.open(this.url)
        }
        /*update_end author:wuxianquan date:20190908 for:判断打开方式，新窗口打开时this.$route.meta.internalOrExternal==true */

      }
    },
  }
}
</script>

<style>
</style>